package plugins.evaluation;

import kd.bos.base.AbstractBasePlugIn;
import kd.bos.cache.CacheFactory;
import kd.bos.cache.DistributeSessionlessCache;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.list.ListShowParameter;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

public class toShowEvalution extends AbstractBasePlugIn implements Plugin {

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);

        if (evt.getItemKey().equalsIgnoreCase("dcb4_showevalution")) {
            // 获取字段数据
            Object nameValue = this.getModel().getValue("name");

            // 获取商户数据包并提取名称
            Object merchant = this.getModel().getValue("dcb4_merchant");
            String merchantName = null;

            if (merchant != null) {
                DynamicObject merchantObj = (DynamicObject) merchant;
                merchantName = merchantObj.getString("name");
            }

            if (nameValue == null || nameValue.toString().isEmpty()) {
                this.getView().showMessage("请先选择菜品");
                return;
            }

            // 存入分布式缓存
            DistributeSessionlessCache cache = CacheFactory.getCommonCacheFactory()
                    .getDistributeSessionlessCache("customRegion");
            cache.put("evaluation_name", nameValue.toString());
            cache.put("evalutaion_merchantName", merchantName);

            // 打开基础资料列表
            ListShowParameter showParameter = new ListShowParameter();
            showParameter.setFormId("bos_list"); // 固定的
            showParameter.setBillFormId("dcb4_evaluation"); // 列表对应的单据字段
            showParameter.getOpenStyle().setShowType(ShowType.Modal); // 打开风格，有非常多种
            this.getView().showForm(showParameter);
        }
    }
}
